package com.rescue.Servlet;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.rescue.toolbean.JDBC;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("OOOOOOOOOOOOO");
		
		try {
			Connection conn=JDBC.getConnection();
			String idnumber= request.getParameter("idnumber");
			String userpwd= request.getParameter("userpwd");
System.out.println(idnumber);
			PreparedStatement pStmt=conn.prepareStatement("select * from user where idnumber='"+ idnumber+"'and userpwd='"+userpwd+"'");
			ResultSet rs=pStmt.executeQuery();
			System.out.println(rs);
			if(rs.next()) {
				response.sendRedirect("MessageBoard.jsp");//TODO 改为救援员首页
			}
			else {
				response.sendRedirect("Login.jsp");
				System.out.println("用户名或密码错误");

			}
			JDBC.closeStament(rs, pStmt, conn);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
